<?php
/**
 * Classe Country
 *
 * Description de la classe
 *
 * @author Mathieu
 */
class Country extends Object {
    protected $id;
    protected $code;
    protected $fr;
    protected $en;
    
    function __construct($id="", $params=array()) {
        if($id!='') {
            $this->id = $id;
            foreach($params as $key => $p) {
                $this->$key = $p;
            }
        }
    }

    function __toString() {
        if(!isset($this->fr)) $this->load();
        return $this->fr."";
    }

    public function load() {
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM pays WHERE id = '".$this->id."'");
        if(sizeof($result) > 0) {
            foreach($result[0] as $key => $p) {
                $this->$key = $p;
            }
            return;
        }
        if(DEBUG) echo("Objet inconnu");
        return false;
    }

    static function getAllCountryOfUsers(){
        $db = Database::getInstance();
        $result = $db->query("SELECT pays.id, pays.code, pays.fr, pays.en FROM pays INNER JOIN users ON pays.id=users.pays_id GROUP BY pays.id");
        foreach($result as $country) {
                $countries[] = new Country($country['id'], array('code'=>$country['code'],
                                                                     'fr'=>$country['fr'],                                                                     'en'=>$country['en']
                                                        ));
        }
        return $countries;

    }

    static function getAllCountry(){
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM pays ORDER BY fr");
        foreach($result as $country) {
                $countries[] = new Country($country['id'], $country);
        }
        return $countries;
    }
}
?>
